<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>洗牌算法可视化解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background-color: #f8fafc;
            color: #1e293b;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 100%);
        }
        .code-block {
            background: #1e293b;
            border-left: 4px solid #8b5cf6;
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-4px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .first-letter {
            font-size: 4rem;
            line-height: 1;
            float: left;
            margin-right: 0.5rem;
            font-weight: 700;
            color: #3b82f6;
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4 md:py-32">
        <div class="max-w-5xl mx-auto text-center">
            <h1 class="text-4xl md:text-6xl font-bold mb-6 font-serif">
                洗牌算法的艺术
            </h1>
            <p class="text-xl md:text-2xl max-w-3xl mx-auto mb-8 font-serif">
                Fisher-Yates算法如何实现真正的随机排列
            </p>
            <div class="flex justify-center space-x-4">
                <button class="bg-white text-indigo-600 px-6 py-3 rounded-full font-medium hover:bg-opacity-90 transition duration-300 transform hover:scale-105">
                    <i class="fas fa-code mr-2"></i> 算法演示
                </button>
                <button class="border-2 border-white text-white px-6 py-3 rounded-full font-medium hover:bg-white hover:bg-opacity-10 transition duration-300 transform hover:scale-105">
                    <i class="fas fa-book mr-2"></i> 了解更多
                </button>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="max-w-4xl mx-auto px-4 py-16">
        <!-- Introduction -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="h-1 bg-indigo-500 w-12 mr-4"></div>
                <h2 class="text-2xl font-bold text-gray-800">算法概述</h2>
            </div>
            <div class="prose prose-lg max-w-none">
                <p><span class="first-letter">洗</span>牌算法是计算机科学中一个经典问题，目的是将一个序列中的元素随机打乱，使得每一种可能的排列组合出现的概率相等。Fisher-Yates算法（也称为Knuth洗牌）是实现这一目标的黄金标准。</p>
                <p>这个算法由Ronald Fisher和Frank Yates于1938年首次提出，后由Donald Knuth在《计算机程序设计艺术》中推广。其核心思想是从后向前遍历数组，在每个位置与一个随机选择的元素交换。</p>
            </div>
        </section>

        <!-- Algorithm Visualization -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="h-1 bg-indigo-500 w-12 mr-4"></div>
                <h2 class="text-2xl font-bold text-gray-800">算法可视化</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <h3 class="text-xl font-semibold mb-4 text-indigo-600 flex items-center">
                        <i class="fas fa-random mr-2"></i> Fisher-Yates 洗牌过程
                    </h3>
                    <div class="mermaid">
                        graph LR
                        A[原始数组] --> B[i = n-1]
                        B --> C[随机选择j: 0 ≤ j ≤ i]
                        C --> D[交换元素i和j]
                        D --> E[i = i-1]
                        E -->|i > 0| C
                        E -->|i ≤ 0| F[洗牌完成]
                    </div>
                </div>
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <h3 class="text-xl font-semibold mb-4 text-indigo-600 flex items-center">
                        <i class="fas fa-chart-bar mr-2"></i> 算法分析
                    </h3>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><strong>时间复杂度:</strong> O(n) - 只需一次线性遍历</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><strong>空间复杂度:</strong> O(1) - 原地操作，无需额外空间</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span><strong>随机性保证:</strong> 每个元素被放置在每个位置的概率相等</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>

        <!-- Implementation -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="h-1 bg-indigo-500 w-12 mr-4"></div>
                <h2 class="text-2xl font-bold text-gray-800">代码实现</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-lg overflow-hidden card-hover">
                <div class="flex items-center bg-gray-800 px-4 py-3">
                    <div class="h-3 w-3 bg-red-500 rounded-full mr-2"></div>
                    <div class="h-3 w-3 bg-yellow-400 rounded-full mr-2"></div>
                    <div class="h-3 w-3 bg-green-500 rounded-full"></div>
                    <div class="ml-auto text-gray-400 text-sm">TypeScript</div>
                </div>
                <div class="code-block p-6 text-gray-200 overflow-x-auto">
                    <pre><code class="language-typescript">function tsShuffle(tsArr: number[]): void {
  for (let i = tsArr.length - 1; i > 0; i--) {
    // 随机选择0到i之间的索引
    const j = Math.floor(Math.random() * (i + 1));
    // 交换当前元素和随机选择的元素
    [tsArr[i], tsArr[j]] = [tsArr[j], tsArr[i]];
  }
}</code></pre>
                </div>
            </div>
            
            <div class="mt-8 prose prose-lg max-w-none">
                <h3 class="text-xl font-semibold mb-4">代码解析</h3>
                <ol class="list-decimal pl-6 space-y-3">
                    <li>从数组末尾开始向前遍历（i从n-1到1）</li>
                    <li>在每次迭代中，随机选择一个索引j（0 ≤ j ≤ i）</li>
                    <li>交换位置i和j的元素</li>
                    <li>重复这个过程直到遍历完整个数组</li>
                </ol>
                <p class="mt-4">这种实现保证了每个元素被放置在任意位置的概率都是1/n，从而实现了真正的随机排列。</p>
            </div>
        </section>

        <!-- Applications -->
        <section>
            <div class="flex items-center mb-8">
                <div class="h-1 bg-indigo-500 w-12 mr-4"></div>
                <h2 class="text-2xl font-bold text-gray-800">应用场景</h2>
            </div>
            
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white rounded-lg p-6 shadow-md card-hover">
                    <div class="text-indigo-500 text-3xl mb-4">
                        <i class="fas fa-gamepad"></i>
                    </div>
                    <h3 class="font-semibold text-lg mb-2">游戏开发</h3>
                    <p class="text-gray-600">用于洗牌、随机生成关卡、敌人分布等需要公平随机的场景</p>
                </div>
                <div class="bg-white rounded-lg p-6 shadow-md card-hover">
                    <div class="text-indigo-500 text-3xl mb-4">
                        <i class="fas fa-chart-pie"></i>
                    </div>
                    <h3 class="font-semibold text-lg mb-2">数据科学</h3>
                    <p class="text-gray-600">机器学习中的数据打乱，确保训练集的随机性</p>
                </div>
                <div class="bg-white rounded-lg p-6 shadow-md card-hover">
                    <div class="text-indigo-500 text-3xl mb-4">
                        <i class="fas fa-music"></i>
                    </div>
                    <h3 class="font-semibold text-lg mb-2">音乐播放</h3>
                    <p class="text-gray-600">实现真正随机的播放列表打乱，避免重复播放</p>
                </div>
            </div>
        </section>
    </div>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>